/**<
8. Make a program that outputs a simple grid based gameboard to the screen
using either numbers or characters. Add enemies that move randomly in any
direction once per turn. (enemies just like traps cause the player to lose
if touched) Allow the user (marked by G in the example) to move either up,
down, left, or right each turn. If the player steps on a trap then they lose.
If the make it to the treasure 'X' then they win.
 */

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

 void SimpleDungeon(){
     //create the gameboard
     int nWidth = 20, nHeight = 20; //set width and heigh of gameboard
     char cGameboard[nWidth][nHeight];  //gameboard is 2D array nWidth X nHeight
     for(int i=0;i<nWidth;i++){
         for(int j=0; j<nHeight;j++) cGameboard[i][j] = '.';  // empty space '.'
     }

     //initialize the player location ('G')
     int nPlayerX = 1, nPlayerY = 1;
     cGameboard[nPlayerX][nPlayerY] = 'G';  //sets the player location as 'G'

     //initialize the Goal (near the bottom left of the gameBoard
     int nGoalX = nWidth -3, nGoalY = nHeight -3;

     //create monsters
     int nNumMonsters = 7;
     int nMonsterX[nNumMonsters], nMonsterY[nNumMonsters];
     srand(time(0));
     for(int i=0;i<nNumMonsters;i++){
        nMonsterX[i] = (rand() % (nWidth)) + 1;
        nMonsterY[i] = (rand() % (nHeight)) + 1;
     }

     //create traps
     int nNumTraps = 3;
     int nTrapX[nNumTraps], nTrapY[nNumTraps];
     for(int i=0;i<nNumTraps;i++){
        nTrapX[i] = (rand() % (nWidth)) + 1;
        nTrapY[i] = (rand() % (nHeight)) + 1;
     }

    char cInput = 0;    //initialize the user input

    //Game Loop
    while(cInput != 'e'){

        //clear the gameboard
        for(int i=0;i<nWidth;i++){
            for(int j=0; j<nHeight;j++) cGameboard[i][j] = '.';  // empty space '.'
        }

        //update the actors location
        cGameboard[nPlayerX][nPlayerY] = 'G';   //player
        cGameboard[nGoalX][nGoalY] = 'X';   //goal
        for(int i=0;i<nNumMonsters;i++){
            cGameboard[nMonsterX[i]][nMonsterY[i]] = 'M';   //all monsters
        }
        for(int i=0;i<nNumTraps;i++){
            cGameboard[nTrapX[i]][nTrapY[i]] = 'T'; //all traps
        }

        //draw the gameboard
        for(int j=0;j<nHeight;j++){
             for(int i =0;i<nWidth;i++){
                cout << cGameboard[i][j];
                if (i == nWidth-1) cout << endl; // new line at width end
             }
         }

         //move the monsters
        for(int i=0;i<nNumMonsters;i++){
            int temp = 0;
            temp = (rand() % 5);
            switch(temp){
                case 1: //up
                    nMonsterY[i]++;
                    break;
                case 2: //down
                    nMonsterY[i]--;
                    break;
                case 3: //left
                    nMonsterX[i]--;
                    break;
                case 4: //right
                    nMonsterX[i]++;
                    break;
            }
            //correct Monster postion if he moves off the screen in x or y
            if (nMonsterX[i] > nWidth-1) nMonsterX[i] = nWidth-1;
            if (nMonsterX[i] < 0) nMonsterX[i] = 0;
            if (nMonsterY[i] > nHeight-1) nMonsterY[i] = nHeight -1;
            if (nMonsterY[i] < 0) nMonsterY[i] = 0;
        }

         //Take input from the user (W,A,S,D for up, left, down, right)

         cout << "Command (W-up,A-left,S-down,D-right, E-Exit): ";
         cin >> cInput;

         //Move the Player
        switch(cInput){
            case 'w':
                if(nPlayerY > 1) nPlayerY--;
                break;
            case 'a':
                if(nPlayerX > 0) nPlayerX--;
                break;
            case 's':
                if(nPlayerY < nHeight-1) nPlayerY++;
                break;
            case 'd':
                if(nPlayerX < nWidth-1) nPlayerX++;
                break;
            case 'e':
                cout << "Game Exit";
                break;
         }

        //check for game win / Lose(eaten by a monster
         if (nPlayerX == nGoalX && nPlayerY == nGoalY){
            cout << "You Won! Good for you.";
            cInput = 'e';   //ends the while loop
            continue;
         }else{     //monster eats you?
            for(int i=0;i<nNumMonsters;i++){
                if(nPlayerX == nMonsterX[i] && nPlayerY == nMonsterY[i]){
                    cout << "You died when a Monster ate you. The End" << endl;
                    cInput = 'e';   //ends the while loop
                    continue;
                }
            }
         }
        //Check for death by trap
        for(int i=0;i<nNumTraps;i++){
            if(nPlayerX == nTrapX[i] && nPlayerY == nTrapY[i]){
                    cout << "You died when you stepped on a trap. The End." << endl;
                    cInput = 'e';    //ends the while loop
                    continue;
            }
        }
    }
}
